package com.shawn.datastructure.array;

public class MergeSortedArray {

	public static int[] merge(int[] num1, int[] num2) {
		if (num1 == null || num1.length == 0) {
			return num2;
		}

		if (num2 == null || num2.length == 0) {
			return num2;
		}

		int[] arr = new int[num1.length + num2.length];
		int m = 0;
		int n = 0;
		while (m < num1.length && n < num2.length) {
			if (num1[m] <= num2[n]) {
				arr[m + n] = num1[m++];
			} else {
				arr[m + n] = num2[n++];
			}
		}
		while (m < num1.length) {
			arr[m + n] = num1[m++];
		}

		while (n < num2.length) {
			arr[m + n] = num2[n++];
		}
		return arr;
	}

	public static void main(String[] args) {
		int[] num1 = {1, 2, 3};
		int[] num2 = {3, 4};
		int[] arr = merge(num1, num2);
		for (int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
	}
}
